// Client.java
import java.io.*;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 12345);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(
                     new InputStreamReader(socket.getInputStream()));
             Scanner scanner = new Scanner(System.in)) {

            System.out.println("已连接到服务器，输入消息发送，输入'退出'结束");
            String userInput;
            while (true) {
                System.out.print("客户端: ");
                userInput = scanner.nextLine();
                out.println(userInput);
                if ("退出".equals(userInput)) {
                    break;
                }
                System.out.println("服务器: " + in.readLine());
            }
        } catch (UnknownHostException e) {
            System.err.println("未知主机: localhost");
        } catch (IOException e) {
            System.err.println("I/O异常: " + e.getMessage());
        }
    }
}
